package com.wehealth.adapter;

import java.util.List;

import com.wehealth.R;
import com.wehealth.dm.DM_Questionnaire_page2_item;
import com.wehealth.dm.DM_Questionnaire_page3_item;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Adapter_questionnaire_page3 extends BaseAdapter{
	
	private Context ctx;
	private List<DM_Questionnaire_page3_item> mList;
	
	//private int  list_pos = 0;
	private int score5= 0;
	private 	int[]  radioButtonID ={R.id.radio_1,R.id.radio_2,R.id.radio_3,R.id.radio_4};
	private 	int[]  radioButtonID9 ={R.id.radio_1,R.id.radio_2,R.id.radio_3,R.id.radio_4,R.id.radio_5};
	int[]  checkboxID ={R.id.quetionnaire_page3_item5_cb1,R.id.quetionnaire_page3_item5_cb2,
			R.id.quetionnaire_page3_item5_cb3,R.id.quetionnaire_page3_item5_cb4,
			R.id.quetionnaire_page3_item5_cb5,R.id.quetionnaire_page3_item5_cb6,
			R.id.quetionnaire_page3_item5_cb7,R.id.quetionnaire_page3_item5_cb8,
			R.id.quetionnaire_page3_item5_cb9};
	
	
	public Adapter_questionnaire_page3(Context context,List<DM_Questionnaire_page3_item> mList){
		ctx =context;
		this.mList =mList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(int pos, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		View view = null;
		//list_pos = pos;
		if(pos!=4 && pos!=8){
			view = getNormalView(pos);
		}else {
			if(pos == 4){
				view =getItem5View(pos);
			}else if(pos ==8){
				view =getItem9View(pos);
			}
		}
		
		
		

		return view;
	}
		
	private View getNormalView(final int index){
		View view = LayoutInflater.from(ctx).inflate(R.layout.questionnaire_page3_list_item,null);
		TextView title = (TextView)view.findViewById(R.id.questionnaire_page3_item_name);
		title.setText(mList.get(index).getTitle());
		

		RadioGroup  radioGroup =(RadioGroup)view.findViewById(R.id.questionnaire_page3_item_radiogroup);
		//RadioButton radioButton1 =(RadioButton)view.findViewById(R.id.radio_1);
		List<String> mlistRaido = mList.get(index).getListRadio();
		for(int i = 0; i<mlistRaido.size();i++){
			RadioButton radioButton =(RadioButton)view.findViewById(radioButtonID[i]);
			radioButton.setText(mlistRaido.get(i));
		}

		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			int  list_pos = index;
			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				// TODO Auto-generated method stub
				
				switch (id) {
				case R.id.radio_1:				
					 checkRadio1(list_pos);		
					 displayDescripRadio1(list_pos);
					break;
				case R.id.radio_2:
					 checkRadio2(list_pos);	
					 displayDescripRadio2(list_pos);
					break;
				case R.id.radio_3:
					 checkRadio3(list_pos);
					 displayDescripRadio3(list_pos);
					break;
				case R.id.radio_4:
					 checkRadio4(list_pos);
					 displayDescripRadio4(list_pos);
					break;
				default:
					break;
				}
				
			}
		});
		
		return view;
	}
	
	private View getItem9View(final int index){
	
		View view = LayoutInflater.from(ctx).inflate(R.layout.questionnaire_page3_list_item9,null);
		TextView title = (TextView)view.findViewById(R.id.questionnaire_page3_item_name);
		title.setText(mList.get(index).getTitle());

		RadioGroup  radioGroup =(RadioGroup)view.findViewById(R.id.questionnaire_page3_item_radiogroup);
		//RadioButton radioButton1 =(RadioButton)view.findViewById(R.id.radio_1);
		List<String> mlistRaido = mList.get(index).getListRadio();
		for(int i = 0; i<mlistRaido.size();i++){
			RadioButton radioButton =(RadioButton)view.findViewById(radioButtonID9[i]);
			radioButton.setText(mlistRaido.get(i));
		}

		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			int list_pos = index;
			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				// TODO Auto-generated method stub
				 int score= 0;
				 String descripStr = null ;
				switch (id) {
				case R.id.radio_1:
					 score =0;
					 descripStr = ctx.getString(R.string.questionaire_page3_item9_ex1);
					
					break;
				case R.id.radio_2:
					 score =5;
					break;
				case R.id.radio_3:
					 score =10;
					break;
				case R.id.radio_4:
					 score =15;
					break;
				case R.id.radio_5:
					 score =20;
					break;
				default:
					break;
				}
				Log.d("SIMMON"," CHECKRadio9 score ="+score+"     list_pos="+list_pos);
				mList.get(list_pos).setScore(score);	
				mList.get(list_pos).setDisplay(descripStr);
			}
		});

		return view;
	}
	
	
	
	
	
	private View getItem5View(final int index){
		View view = LayoutInflater.from(ctx).inflate(R.layout.questionnaire_page3_list_item5,null);
		TextView title = (TextView)view.findViewById(R.id.questionnaire_page3_item_name);
		title.setText(mList.get(index).getTitle());

		List<String> mListString = mList.get(index).getListRadio();
		int lengh = checkboxID.length;
		for(int k =0; k <lengh;k++){
			CheckBox  cb = (CheckBox)view.findViewById(checkboxID[k]);
			cb.setText(mListString.get(k));
			cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				int list_pos = index;
				
				@Override
				public void onCheckedChanged(CompoundButton button, boolean state) {
					// TODO Auto-generated method stub
					int id =button.getId();
					Log.d("SIMMON","  onCheckedChanged= "+state);
					switch (id) {
					case R.id.quetionnaire_page3_item5_cb1:
					case R.id.quetionnaire_page3_item5_cb3:
					case R.id.quetionnaire_page3_item5_cb7:
					case R.id.quetionnaire_page3_item5_cb8:
						if(state){
							score5 +=5;
						}else{
							score5 -=5;
						}
						break;
					case R.id.quetionnaire_page3_item5_cb2:
					case R.id.quetionnaire_page3_item5_cb4:
						if(state){
							score5 +=15;
							}else{
								score5 -=15;
							}
						break;
					case R.id.quetionnaire_page3_item5_cb5:
					case R.id.quetionnaire_page3_item5_cb6:
						if(state){
							score5 +=10;
							}else{
								score5 -=10;
							}
						break;
					case R.id.quetionnaire_page3_item5_cb9:
						 
						break;
					default:
						break;
					}
					Log.d("SIMMON"," CHECKRadio5score ="+score5+"     list_pos="+list_pos);
					mList.get(list_pos).setScore(score5);
					
				}
			});
		}
		return view;
	}

	
	
	private void  checkRadio1(int list_pos){
		int score =0;
		switch (list_pos) {
			case 0:
			case 1:
				score =10;
				break;
			case 2:
			case 3:
				score =15;
				break;
			case 5:
			case 9:
				score =30;
				break;
			case 6:
			case 7:
				score =0;
				break;
		
		}
		Log.d("SIMMON"," CHECKRadio1 score ="+score+"     list_pos="+list_pos);
		mList.get(list_pos).setScore(score);
		
	}
	
	private void checkRadio2(int list_pos){
		int score =0;
		switch (list_pos) {
		case 0:
		case 2:
		case 3:
			score =30;
			break;
		case 1:
		case 5:
		case 9:
			score =20;
			break;
		case 6:
			score =10;
			break;
		case 7:
			score =0;
			break;
	
	}
		Log.d("SIMMON"," CHECKRadio2 score ="+score+"     list_pos="+list_pos);
	mList.get(list_pos).setScore(score);
	}
	
	
	private void checkRadio3(int list_pos){
		int score =0;
		switch (list_pos) {
		case 0:
			score =20;
			break;
		case 1:
			score =30;
			break;
		case 2:
		case 3:
		case 7:
			score =15;
			break;
		case 5:
		case 9:
			score =10;
			break;
		case 6:
			score =20;
			break;
	}
		Log.d("SIMMON"," CHECKRadio3 score ="+score+"     list_pos="+list_pos);
	mList.get(list_pos).setScore(score);
	}
	
	private void checkRadio4(int list_pos){
		int score =0;
		switch (list_pos) {
		case 0:
		case 1:
		case 3:
		case 5:
			score =0;
			break;
		case 6:
			score =30;
			break;	
		case 7:
			score =35;
			break;
		case 9:
			score =0;
			break;
	
		}
		Log.d("SIMMON"," CHECKRadio4 score ="+score+"     list_pos="+list_pos);
	mList.get(list_pos).setScore(score);
	}
	
	
	private void displayDescripRadio1(int list_pos){
		String  descripStr=null;
		switch (list_pos) {
			case 6:
				descripStr = ctx.getString(R.string.questionaire_page3_item7_ex1);
			break;
			case 7:
				descripStr = ctx.getString(R.string.questionaire_page3_item8_ex12);
			break;
		}
		mList.get(list_pos).setDisplay(descripStr);  
	}
	
	
	private void displayDescripRadio2(int list_pos){
		String  descripStr=null;
		switch (list_pos) {
			case 7:
				descripStr = ctx.getString(R.string.questionaire_page3_item8_ex12);
				
			break;
		}
		mList.get(list_pos).setDisplay(descripStr);     
	}
	
	private void displayDescripRadio3(int list_pos){
		String  descripStr=null;
		switch (list_pos) {
			case 1:
				descripStr = ctx.getString(R.string.questionaire_page3_item2_ex3);
			break;
			case 2:
				descripStr = ctx.getString(R.string.questionaire_page3_item3_ex3);
				
			break;
		}
		mList.get(list_pos).setDisplay(descripStr);     
	}
	
	private void displayDescripRadio4(int list_pos){
		String  descripStr=null;
		switch (list_pos) {
			case 0:
				descripStr = ctx.getString(R.string.questionaire_page3_item1_ex);
			break;
			case 1:
				descripStr = ctx.getString(R.string.questionaire_page3_item2_ex4);
				break;
			case 2:
				descripStr = ctx.getString(R.string.questionaire_page3_item3_ex4);

			break;
			case 3:
				descripStr = ctx.getString(R.string.questionaire_page3_item4_ex4);
	
			break;
			case 5:
				descripStr = ctx.getString(R.string.questionaire_page3_item6_ex4);

			break;
			case 9:
				descripStr = ctx.getString(R.string.questionaire_page3_item10_ex);
				
			break;
		}
		mList.get(list_pos).setDisplay(descripStr);    
	}
	
	
	

}
